// import Vue from 'vue';
// import VueRouter from 'vue-router';
import Operation from '../views/Operation/Index.vue';
document.title = '鸿惠丰纸箱管理系统';

const originalPush = VueRouter.prototype.push
  VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    component: Operation,
    meta: {
      title: 'Operation'
    },
    children: [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Operation/Home.vue'),
        meta: {
          title: 'Home'
        },
      },
      {
        path: 'client',
        component: () => import('@/views/Operation/Client/Client.vue'),
        meta: {
          title: 'Client'
        },
        children: [
          {
            path: '',
            name: 'ClientIndex',
            component: () => import('@/views/Operation/Client/ClientIndex.vue'),
            meta: {
              title: 'Client'
            }
          },
          {
            path: 'check/:id',
            name: 'ClientCheck',
            component: () => import('@/views/Operation/Client/ClientCheck.vue'),
            meta: {
              title: 'Client'
            }
          },
          {
            path: 'append',
            name: 'ClientAppend',
            component: () => import('@/views/Operation/Client/ClientAppend.vue'),
            meta: {
              title: 'Client'
            }
          }
        ]
      },
      {
        path: 'order',
        component: () => import('@/views/Operation/Order/Order.vue'),
        meta: {
          title: 'Order'
        },
        children: [
          {
            path: '',
            name: 'OrderIndex',
            component: () => import('@/views/Operation/Order/OrderIndex.vue'),
            meta: {
              title: 'Order'
            }
          },
          {
            path: 'check/:id',
            name: 'OrderCheck',
            component: () => import('@/views/Operation/Order/OrderCheck.vue'),
            meta: {
              title: 'Order'
            }
          },
          {
            path: 'append/:clientId',
            name: 'OrderAppend',
            component: () => import('@/views/Operation/Order/OrderAppend.vue'),
            meta: {
              title: 'Order'
            }
          },
        ]
      },
      {
        path: 'delivery',
        component: () => import('@/views/Operation/Delivery/Delivery.vue'),
        meta: {
          title: 'Delivery'
        },
        children: [
          {
            path: '',
            name: 'DeliveryIndex',
            component: () => import('@/views/Operation/Delivery/DeliveryIndex.vue'),
            meta: {
              title: 'Delivery'
            }
          },
          {
            path: 'check/:id',
            name: 'DeliveryCheck',
            component: () => import('@/views/Operation/Delivery/DeliveryCheck.vue'),
            meta: {
              title: 'Delivery'
            }
          },
          {
            path: 'append/:clientId',
            name: 'DeliveryAppend',
            component: () => import('@/views/Operation/Delivery/DeliveryAppend.vue'),
            meta: {
              title: 'Delivery'
            }
          }
        ]
      },
      {
        path: 'summary',
        name: 'Summary',
        component: () => import('@/views/Operation/Summary/Summary.vue'),
        meta: {
          title: 'Summary'
        }
      },
      {
        path: 'about',
        name: 'About',
        component: () => import('@/views/Operation/About/AboutIndex.vue'),
        meta: {
          title: 'About'
        }
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: 'Login'
    }
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import('@/views/Test.vue'),
    meta: {
      title: 'Test'
    }
  }
  
];


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && !localStorage.token) {
    return next('Login');
  }
   next()
})

export default router;
